package com.aabte.springcloudlearn.eurekaserverproviderdemo.controller;

import com.aabte.springcloudlearn.common.beans.User;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Daniel
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserProviderController {

    private List<User> users = Lists.newArrayList(
            User.builder().id(1L).username("Tom").sex("man").phone("13111111111").region("south").build(),
            User.builder().id(2L).username("Juliet").sex("woman").phone("13122222222").region("north").build(),
            User.builder().id(3L).username("Romeo").sex("man").phone("13133333333").region("east").build()
    );

    @GetMapping
    public List<User> queryUsers(
            @RequestParam(value = "ids", required = false) List<Long> ids
    ) {
        log.info("Start queryUsers. ids: {}", ids);
        List<User> result;
        if (null == ids) {
            result = users;
        } else {
            result = users.stream().filter(user -> ids.contains(user.getId())).collect(Collectors.toList());
        }
        log.info("End queryUsers. result: {}", result);
        return result;
    }

    @GetMapping("/{id:\\d+}")
    public User queryUserById(
            @PathVariable(name = "id") Long id) {
        log.info("Start queryUserById. id: {}", id);
        User result = getUserById(id);
        log.info("End queryUserById. result: {}", result);
        return result;
    }

    @PostMapping
    public User registerUser(
            @RequestBody @Valid User user) {
        log.info("Start registerUser. user: {}", user);
        User tmp = getUserByUsername(user.getUsername());
        User result = null;
        if (null == tmp) {
            user.setId(users.get(users.size() - 1).getId() + 1);
            users.add(user);
            result = user;
        }
        log.info("End registerUser. result: {}", result);
        return result;
    }

    @PutMapping
    public User updateUser(
            @RequestBody @Valid User user) {
        log.info("Start updateUser. user: {}", user);
        User tmp = getUserById(user.getId());
        User result = null;
        if (null != tmp) {
            BeanUtils.copyProperties(user, tmp);
            result = user;
        }
        log.info("End updateUser. result: {}", result);
        return result;
    }

    @DeleteMapping("/{id:\\d+}")
    public User deleteUserById(
            @PathVariable(name = "id") Long id) {
        log.info("Start queryUserById. id: {}", id);
        User result = null;

        Iterator<User> iterator = users.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getId().equals(id)) {
                iterator.remove();
                result = user;
            }
        }
        log.info("End queryUserById. id: {}", id);
        return result;
    }

    @GetMapping("/header")
    public List<User> getUsersByRegion(
            @RequestHeader(value = "region") String region
    ){
        log.info("Start getUsersByRegion. region: {}", region);
        List<User> result = users.stream().filter(user -> user.getRegion().equalsIgnoreCase(region)).collect(Collectors.toList());
        log.info("End getUsersByRegion. result: {}", result);
        return result;
    }

    private User getUserById(Long id) {
        for (User user : users) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }

    private User getUserByUsername(String username) {
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                return user;
            }
        }
        return null;
    }

}
